<template>
  <div class="ele-body">
    <el-card shadow="never">
      <div slot="header" class="clearfix">
        <span>{{ $t("route.system.menu._name") }}</span>
      </div>
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        class="ele-form-search"
        label-width="75px"
        @keyup.enter.native="reload"
        @submit.native.prevent
      >
        <div class="app-container">
          <div class="filter-container">
            <el-input
              v-model="where.title"
              class="filter-item"
              clearable
              placeholder="菜单名称"
              style="width: 180px"
              @clear="reload"
            />

            <el-input
              v-model="where.path"
              class="filter-item"
              clearable
              placeholder="菜单地址"
              style="width: 180px"
              @clear="reload"
            />

            <el-button
              class="filter-item"
              icon="el-icon-search"
              type="primary"
              @click="reload"
              >搜索
            </el-button>

            <el-button
              class="filter-item"
              icon="el-icon-refresh"
              style="margin-left: 2px"
              @click="reset"
              >重置
            </el-button>
          </div>
        </div>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="url"
        :default-expand-all="false"
        :max-height="defaultHeight"
        :need-page="false"
        :parse-data="parseData"
        :where="where"
        row-key="menuId"
      >
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="openEdit({})"
            >添加
          </el-button>
          <el-button size="small" @click="expandAll">展开全部 </el-button>
          <el-button size="small" @click="foldAll">折叠全部 </el-button>
        </template>
        <!-- 标题列 -->
        <template slot="title" slot-scope="{ row }">
          <i :class="row.icon"></i> {{ row.title }}
        </template>
        <!-- 类型列 -->
        <template slot="menuType" slot-scope="{ row }">
          <el-tag v-if="isUrl(row.path)" size="mini" type="warning"
            >外链
          </el-tag>
          <el-tag v-else-if="isUrl(row.component)" size="mini" type="success"
            >内链
          </el-tag>
          <el-tag v-else :type="['primary', 'info'][row.menuType]" size="mini">
            {{ ["菜单", "按钮"][row.menuType] }}
          </el-tag>
        </template>
        <!-- 级别列 -->
        <template slot="menuLevel" slot-scope="{ row }">
          <el-tag :type="['success', 'danger'][row.menuLevel]" size="mini">
            {{ ["用户", "系统"][row.menuLevel] }}
          </el-tag>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{ row }">
          <el-link
            :underline="false"
            icon="el-icon-plus"
            type="primary"
            @click="openEdit(null, row.menuId)"
            >添加
          </el-link>
          <el-link
            :underline="false"
            icon="el-icon-edit-outline"
            type="primary"
            @click="openEdit(row)"
            >修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除吗？"
            @confirm="remove(row)"
          >
            <el-link
              slot="reference"
              :underline="false"
              icon="el-icon-delete"
              type="danger"
              >删除
            </el-link>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <menu-edit
      :data="current"
      :menu-list="menuList"
      :visible.sync="showEdit"
      @done="reload"
    />
  </div>
</template>

<script>
import MenuEdit from "./menu-edit";

export default {
  name: "SystemMenu",
  components: { MenuEdit },
  data() {
    return {
      // 表格高度
      defaultHeight: 0,
      // 表格数据接口
      url: "/sys/menu",
      // 表格列配置
      columns: [
        {
          columnKey: "index",
          type: "index",
          width: 45,
          align: "center",
          showOverflowTooltip: true,
        },
        {
          prop: "title",
          label: "菜单名称",
          showOverflowTooltip: true,
          minWidth: 110,
          slot: "title",
        },
        {
          prop: "menuLevel",
          label: "菜单级别",
          showOverflowTooltip: true,
          width: 80,
          slot: "menuLevel",
        },
        {
          prop: "path",
          label: "路由地址",
          showOverflowTooltip: true,
          minWidth: 110,
        },
        {
          prop: "component",
          label: "组件路径",
          showOverflowTooltip: true,
          minWidth: 110,
        },
        {
          prop: "authority",
          label: "权限标识",
          showOverflowTooltip: true,
          minWidth: 110,
        },
        {
          prop: "sortNumber",
          label: "排序",
          align: "center",
          showOverflowTooltip: true,
          width: 60,
        },
        {
          prop: "hide",
          label: "可见",
          align: "center",
          showOverflowTooltip: true,
          width: 60,
          formatter: (row, column, cellValue) => {
            return ["是", "否"][cellValue];
          },
        },
        {
          prop: "menuType",
          label: "类型",
          align: "center",
          showOverflowTooltip: true,
          width: 60,
          slot: "menuType",
        },
        {
          prop: "createTime",
          label: "创建时间",
          showOverflowTooltip: true,
          minWidth: 110,
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          },
        },
        {
          columnKey: "action",
          label: "操作",
          width: 190,
          resizable: false,
          slot: "action",
        },
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 全部菜单数据
      menuList: [],
    };
  },
  created() {
    window.addEventListener("resize", this.getHeight);
    this.getHeight();
  },

  methods: {
    /*设置表格高度*/
    getHeight() {
      this.defaultHeight = window.innerHeight - 320;
    },
    /* 解析接口返回数据 */
    parseData(res) {
      res.data = this.$util.toTreeData(res.data, "menuId", "parentId");
      this.menuList = res.data;
      return res;
    },

    /* 刷新表格 */
    reload(isPage) {
      let obj = { where: this.where };
      if (!isPage) {
        obj.page = 1;
      }
      this.$refs.table.reload(obj);
    },

    /* 重置搜索 */
    reset() {
      for (let key in this.where) {
        this.where[key] = null;
      }
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row, parentId) {
      this.current = Object.assign(
        {
          menuType: 0,
          hide: 0,
          parentId: parentId,
        },
        row
      );
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      if (row.children && row.children.length > 0) {
        this.$message.error("请先删除子节点");
        return;
      }
      const loading = this.$loading({ lock: true });
      this.$http
        .delete("/sys/menu/id/" + row.menuId)
        .then((res) => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch((e) => {
          loading.close();
          this.$message.error(e.message);
        });
    },
    /* 展开全部 */
    expandAll() {
      this.$refs.table.data.forEach((d) => {
        this.$refs.table.toggleRowExpansion(d, true);
      });
    },
    /* 折叠全部 */
    foldAll() {
      this.$refs.table.data.forEach((d) => {
        this.$refs.table.toggleRowExpansion(d, false);
      });
    },
    /* 判断是否是网址 */
    isUrl(url) {
      return (
        url &&
        (url.startsWith("http://") ||
          url.startsWith("https://") ||
          url.startsWith("://"))
      );
    },
  },
};
</script>
